package com.wang.myblog.controller;


import com.wang.myblog.entity.BoardMes;
import com.wang.myblog.entity.Comment;
import com.wang.myblog.entity.vo.BoardMesFrom;
import com.wang.myblog.entity.vo.CommentFrom;
import com.wang.myblog.service.BoardMesService;
import com.wang.myblog.service.CommentService;
import com.wang.myblog.utils.R;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 留言板 前端控制器
 * </p>
 *
 * @author s
 * @since 2021-05-21
 */
@RestController
@RequestMapping("/myblog/boardMes")
public class BoardMesController {
    @Autowired
    private BoardMesService boardMesService;

    @ApiOperation(value = "添加留言板")
    @PostMapping("add")
    public R add(@ApiParam(name = "comment", value = "留言信息", required = true)
                 @RequestBody @Validated BoardMesFrom boardMes) {
        if (StringUtils.isEmpty(boardMes.getPid())) {
            boardMes.setPid(0L);//-前端不传或者传0，则表示为一级评论
        } else {
            if (StringUtils.isEmpty(boardMes.getReplyUser())) {
                return R.error("用户名不能为空！");
            }
        }
        BoardMes commentDb = new BoardMes();
        BeanUtils.copyProperties(boardMes, commentDb);

        return R.add(boardMesService.save(commentDb));
    }
}

